// pages/register/register.js
const computedBehavior = require('miniprogram-computed').behavior

import {
  Request
} from '../../http/http.js'
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
Page({
  behaviors: [computedBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    key: Date.now() + '', // 获取当前的时间戳
    picCode: '', // 图形验证码
    mobile: '', //手机号
    pwd: '', //密码
    password: '', //确认密码
    code: '', //短信验证码
    nick: '', //用户名
    // 地址
    value: [],
    // 倒计时
    flag: true,
    // 倒计时
    time: 0,
    error: {
      picCode: '', // 图形验证码
      mobile: '', //手机号
      pwd: '', //密码
      password: '', //确认密码
      code: '', //短信验证码
      nick: '', //用户名
    },
    flag: false, // 是否显示密码
    sureFlag: false, // 是否显示确认密码
  },
  // 切换密码
  handleflag() {
    this.data.flag = !this.data.flag
    console.log(this.data.flag, 111);
    this.setData({
      flag: this.data.flag
    })
  },
  sureFlag() {
    this.data.sureFlag = !this.data.sureFlag
    this.setData({
      sureFlag: this.data.sureFlag
    })
  },
  // 切换图形验证码
  keyClick() {
    this.setData({
      key: Date.now()
    })
  },
  // 地址
  onChange(e) {
    console.log(e);
    this.setData({
      value: e.detail.value
    })
  },
  // 发送短信验证码
  codeClick() {
    let message = ''
    if (!/^1[3-9]\d{9}$/.test(this.data.mobile)) {
      message = '手机格式不正确'
    } else if (this.data.picCode.length !== 6) {
      message = '请输入图形验证码'
    }
    if (message !== '') {
      Notify(message)
      return;
    }

    // 倒计时
    let flag = this.data.flag
    flag = !flag
    let newtime = 60
    let timer = setInterval(() => {
      if (newtime == 0) {
        clearTimeout(timer)
        flag = !flag
      }
      newtime -= 1
      this.setData({
        time: newtime,
        flag
      })
    }, 1000);
    Request({
      url: 'verification/sms/get',
      data: {
        mobile: this.data.mobile,
        key: this.data.key,
        picCode: this.data.picCode
      }
    }).then(res => {

      if (res.code === 0) {
        Notify({
          type: 'success',
          message: '发送短信成功'
        });
      } else {
        Notify(res.msg)
      }

    })

  },
  // 注册
  registerClick() {
    // 验证表单
    this.data.error = {}
    let tag = true
    if (!/^1[3-9]\d{9}$/.test(this.data.mobile)) {
      this.data.error.mobile = '手机格式错误'
      tag = false
    } else if (!/^\w{6,16}$/.test(this.data.pwd)) {
      console.log(111);
      this.data.error.pwd = '密码长度在6-16位之间'
      tag = false
    } else if (this.data.pwd !== this.data.password) {
      this.data.error.password = '两次输入密码不同'
      tag = false
    } else if (this.data.nick.length == 0) {
      this.data.error.nick = '请输入用户名'
      tag = false
    } else if (this.data.picCode.length == 0) {
      this.data.error.picCode = '请输入图形验证码'
      tag = false
    } else if (this.data.code.toString().length !== 4) {
      this.data.error.code = '请输入短信验证码'
      tag = false
    }
    this.setData({
      error: this.data.error
    })
    if (!tag) return;
    Request({
      url: 'user/m/register',
      method: 'POST',
      data: {
        mobile: this.data.mobile,
        pwd: this.data.pwd,
        code: this.data.code
      }
    }).then(res => {
      Notify({
        type: 'success',
        message: '注册成功'
      });
    }).catch(err => {

      Notify({
        type: 'warning',
        message: '已注册'
      });
    })
  },
  // 跳转登陆页面
  loginClick() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  computed: {
    // 获取图片验证码
    picCodeUrl(data) {
      return `https://api.it120.cc/small4/verification/pic/get?key=${data.key}`
    },

  },
})